const path = require('path');
const fs = require('fs');
const electron = require('electron');

const DEFAULT_CONFIG = {
    mysql: {
        host: 'localhost',
        port: '3306',
        user: '',
        password: '',
        database: ''
    }
}

const CONFIG_PATH = path.join(electron.app.getPath('appData'), electron.app.getName(), 'config.json');
const CONFIG_TMP_PATH = path.join(electron.app.getPath('appData'), electron.app.getName(), '.config.json');

function write(config = {}) {
    Object.assign(DEFAULT_CONFIG, config);
    fs.writeFileSync(CONFIG_TMP_PATH, JSON.stringify(DEFAULT_CONFIG));
    fs.cpSync(CONFIG_TMP_PATH, CONFIG_PATH);
    fs.unlink(CONFIG_TMP_PATH, () => {});
}

(function () {
    if (fs.existsSync(CONFIG_PATH)) {
        const loaclConfig = JSON.parse(fs.readFileSync(CONFIG_PATH).toString());
        Object.assign(DEFAULT_CONFIG, loaclConfig);
    } else {
        write();
    }
})()

const ASE_KEY = '0B414FE526A356D7';

module.exports = {
    DEFAULT_CONFIG,
    write,
    ASE_KEY,
};